package com.example.pushapplication.live.channel;

import android.app.Activity;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;

import com.example.pushapplication.live.LivePusher;
public class VideoChannel implements CameraHelper.OnChangeSizeListener, Camera.PreviewCallback {
    public static final String TAG = "VideoChannel";
    private LivePusher mLivePusher;
    private int bitrate;
    private int fps;
    private CameraHelper cameraHelper;

    public VideoChannel(LivePusher livePusher, Activity activity,
                        int width, int height, int bitrate, int fps, int cameraId) {
        mLivePusher = livePusher;
        this.bitrate = bitrate;
        this.fps = fps;
        cameraHelper = new CameraHelper(activity, cameraId, width, height);

        //1、让camerahelper的
        cameraHelper.setPreviewCallback(this);
        //2、回调 真实的摄像头数据宽、高
        cameraHelper.setOnChangeSizeListener(this);
    }
    public void setPreviewDisplay(SurfaceHolder surfaceHolder) {
        cameraHelper.setPreviewDisplay(surfaceHolder);
    }

    @Override
    public void onChange(int width, int height) {
        //返回真实的宽和高

        //初始化编码器   在打开相机的时候就被调用
        mLivePusher.native_setVideoEncInfo(width, height, fps, bitrate);
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        //相机返回的数据   把数据回调到外层
        Log.i(TAG, "onPreviewFrame: "+data);
        //发送数据
        if (isLiving){
            mLivePusher.native_pushVideo(data);
        }
    }
    private boolean isLiving;
    public void startLive() {
        isLiving = true;

    }

    public void stopLive() {
        isLiving = false;
    }
}
